<?php
/**
 * User: Allen
 * Date: 15-07-13
 * Time: 14:00
 * 二级项目
 */

class Submenu extends PC_Controller{
	
	public function __construct(){
		parent::__construct();
		$this->load->model('submenu_model');
		$this->load->model('project_model');
	}
	
	//二级列表
	public function submenu_list(){
		//获取参数
		$id = $this->uri->segment(4);
		if($id) {
			$where['project_id'] = $id;
		}else {
			$where = array();
		}
		//获取总数
	 	$this->data['count'] = $this->submenu_model->getCount($where);
		if($this->data['count']) {
			$this->data['data'] = $this->submenu_model->submenu_detail($where);
		}else {
			$this->data['data'] = array();
		}
		$this->load->view('/www/submenu_list.php',$this->data);
	}
	
	//读取信息进入编辑页面
	public function edit_page() {
		$id = $this->uri->segment(4);
		$where['id'] = $id;
		$project_info = $this->submenu_model->checkSubmenu($where);
		$this->data['data'] = $this->project_model->getProjectlist();
		$this->data['html'] = $project_info;
		$this->load->view('/www/submenu_edit',$this->data);
	}
	
	//删除操作
	public function del() {
		$id = $this->uri->segment(4);
		$where['id'] = $id;	
		$del_result = $this->submenu_model->deleteSubmenu($where);
		if($del_result) {
			echo "<script>alert('删除成功');window.location.replace('/www/submenu/submenu_list');</script>";	
		}else {
			echo "<script>alert('删除失败');window.location.replace('/www/submenu/submenu_list');</script>";	
		}
		header("Location:http://".base_url()."www/submenu/submenu_list");
	}
	
	//编辑操作
	public function edit() {		
		$data = $_POST;
		$data['update_time'] = time();
		$where['id'] = $data['id'];
		$edit_result = $this->submenu_model->editSubmenu($where,$data);	
		if($edit_result){
			echo "<script>alert('编辑成功');window.location.replace('/www/submenu/submenu_list');</script>";			
		}else{ 
			echo "<script>alert('编辑失败');window.location.replace('/www/submenu/submenu_list');</script>";
		}
	}
	
	//进入添加页面
	public function add_page() {
		$this->data['data'] = $this->project_model->getProjectlist();
		$this->load->view('/www/submenu_add',$this->data);
	}

	//添加操作
	public function add() {
		$data = $_POST;
		$data['create_time'] = $data['update_time'] = time();
		$add_result = $this->submenu_model->addSubmenu($data);
		if($add_result) {
			echo "<script>alert('添加成功');window.location.replace('/www/submenu/submenu_list');</script>";		
		}else {
			echo "<script>alert('添加失败');window.location.replace('/www/submenu/submenu_list');</script>";
		}
	}
}